//
//  YLMonthlyBillHeaderBottomView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/25.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMonthlyBillHeaderBottomView.h"
#import "PNChart.h"
#import "PNLineChart.h"
#import "YLMonthlyBillBrokenLineViewModel.h"
@interface YLMonthlyBillHeaderBottomView ()<PNChartDelegate>
@property (nonatomic, strong) PNLineChart *lineChart;
@property (nonatomic, strong) YLMonthlyBillBrokenLineViewModel *viewModel;


@end

@implementation YLMonthlyBillHeaderBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (YLMonthlyBillBrokenLineViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLMonthlyBillBrokenLineViewModel alloc] init];
    }
    return _viewModel;
}

- (NSArray *)reversedArray:(NSArray *)array
{
   return [[array reverseObjectEnumerator] allObjects];
}

- (void)setType:(YLMonthlyBillHeaderBottomViewType)type
{
    _type = type;
    if (type == YLMonthlyBillHeaderBottomViewTypeHoldingAssets) {
        [self holdingAssets];
    } else if (type == YLMonthlyBillHeaderBottomViewTypeExpectedReturn) {
        [self expectedReturn];
    }
}

// 预期收益
- (void)expectedReturn
{
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.expectedReturnCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        weakSelf.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        weakSelf.lineChart.appendString = @"(元)";
        //self.lineChart.showCoordinateAxis = YES;
        weakSelf.lineChart.yLabelFormat = @"%1.3f";
        weakSelf.lineChart.showYGridLines = NO;
        weakSelf.lineChart.showGenYLabels = NO;
        weakSelf.lineChart.showSmoothLines = NO;
        //    self.lineChart.xLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:8.0];
        [weakSelf.lineChart setXLabels:weakSelf.viewModel.months];
        weakSelf.lineChart.yLabelColor = [UIColor blackColor];
        weakSelf.lineChart.xLabelColor = [UIColor blackColor];
        
        weakSelf.lineChart.yFixedValueMin = 0;
        
        weakSelf.lineChart.xLabelFont = [UIFont boldSystemFontOfSize:13.0f];
        weakSelf.lineChart.xLabelColor = [UIColor blackColor];
        
        //
        NSArray *data01Array = [self reversedArray:weakSelf.viewModel.investmentTotals];
        data01Array = [[data01Array reverseObjectEnumerator] allObjects];
        PNLineChartData *data01 = [PNLineChartData new];
        
        //        data01.rangeColors = @[
        //                [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(10, 30) color:[UIColor redColor]],
        //                [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(100, 200) color:[UIColor purpleColor]]
        //        ];
        //    data01.dataTitle = @"Alpha";
        data01.color = PNFreshGreen;
        data01.pointLabelColor = [UIColor blackColor];
        data01.alpha = 0.3f;
        data01.showPointLabel = YES ;
        data01.pointLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:9.0];
        data01.itemCount = data01Array.count;
        data01.inflexionPointColor = PNRed;
        data01.inflexionPointStyle = PNLineChartPointStyleCircle;
        data01.getData = ^(NSUInteger index) {
            CGFloat yValue = [data01Array[index] floatValue];
            return [PNLineChartDataItem dataItemWithY:yValue];
        };
        
        
        
        weakSelf.lineChart.chartData = @[data01];
        [weakSelf.lineChart.chartData enumerateObjectsUsingBlock:^(PNLineChartData *obj, NSUInteger idx, BOOL *stop) {
            obj.pointLabelColor = [UIColor blackColor];
        }];
        
        
        [weakSelf.lineChart strokeChart];
        weakSelf.lineChart.delegate = weakSelf;
        
        
        [weakSelf addSubview:weakSelf.lineChart];
        
        weakSelf.lineChart.legendStyle = PNLegendItemStyleSerial;
        weakSelf.lineChart.legendFont = [UIFont boldSystemFontOfSize:12.0f];
        weakSelf.lineChart.legendFontColor = [UIColor redColor];
    }];
    
}

// 持有资产
- (void)holdingAssets
{
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        
        weakSelf.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        weakSelf.lineChart.appendString = @"(万元)";
        //self.lineChart.showCoordinateAxis = YES;
        weakSelf.lineChart.yLabelFormat = @"%1.3f";
        weakSelf.lineChart.showYGridLines = NO;
        weakSelf.lineChart.showGenYLabels = NO;
        weakSelf.lineChart.showSmoothLines = NO;
        //    self.lineChart.xLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:8.0];
        [weakSelf.lineChart setXLabels:weakSelf.viewModel.months];
        weakSelf.lineChart.yLabelColor = [UIColor blackColor];
        weakSelf.lineChart.xLabelColor = [UIColor blackColor];
        
        weakSelf.lineChart.yFixedValueMin = 0;
        
        weakSelf.lineChart.xLabelFont = [UIFont boldSystemFontOfSize:13.0f];
        weakSelf.lineChart.xLabelColor = [UIColor blackColor];
        
        //
        NSArray *data01Array = [self reversedArray:weakSelf.viewModel.investmentTotals];
        data01Array = [[data01Array reverseObjectEnumerator] allObjects];
        PNLineChartData *data01 = [PNLineChartData new];
        
        //        data01.rangeColors = @[
        //                [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(10, 30) color:[UIColor redColor]],
        //                [[PNLineChartColorRange alloc] initWithRange:NSMakeRange(100, 200) color:[UIColor purpleColor]]
        //        ];
        //    data01.dataTitle = @"Alpha";
        data01.color = PNFreshGreen;
        data01.pointLabelColor = [UIColor blackColor];
        data01.alpha = 0.3f;
        data01.showPointLabel = YES ;
        data01.pointLabelFont = [UIFont fontWithName:@"Helvetica-Light" size:9.0];
        data01.itemCount = data01Array.count;
        data01.inflexionPointColor = PNRed;
        data01.inflexionPointStyle = PNLineChartPointStyleCircle;
        data01.getData = ^(NSUInteger index) {
            CGFloat yValue = [data01Array[index] floatValue];
            return [PNLineChartDataItem dataItemWithY:yValue];
        };
        
        
        
        weakSelf.lineChart.chartData = @[data01];
        [weakSelf.lineChart.chartData enumerateObjectsUsingBlock:^(PNLineChartData *obj, NSUInteger idx, BOOL *stop) {
            obj.pointLabelColor = [UIColor blackColor];
        }];
        
        
        [weakSelf.lineChart strokeChart];
        weakSelf.lineChart.delegate = weakSelf;
        
        
        [weakSelf addSubview:weakSelf.lineChart];
        
        weakSelf.lineChart.legendStyle = PNLegendItemStyleSerial;
        weakSelf.lineChart.legendFont = [UIFont boldSystemFontOfSize:12.0f];
        weakSelf.lineChart.legendFontColor = [UIColor redColor];
    }];

    
}

- (void)setUpUI
{
//    [self expectedReturn];
//    [self holdingAssets];
}

- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex {
    NSLog(@"Click Key on line %f, %f line index is %d and point index is %d", point.x, point.y, (int) lineIndex, (int) pointIndex);
}

- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex {
    NSLog(@"Click on line %f, %f, line index is %d", point.x, point.y, (int) lineIndex);
}


- (void)userClickedOnBarAtIndex:(NSInteger)barIndex {
}




@end
